package com.Server.DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class OkIpTable {
	
	private static Logger _log = Logger.getLogger(OkIpTable.class.getName());
	
	private static ArrayList<String> _okip;

	private static ArrayList<String> _host;//TODO ban_ipテーブルの不具合を修正
	
	private static ArrayList<String>_online ;
	
	public static boolean isInitialized;
	
	private static OkIpTable _instance;
	
	public static OkIpTable get(){
		if(_instance == null){
			_instance = new OkIpTable();
			_okip = new ArrayList<String>();
			_host = new ArrayList<String>();//TODO ban_ipテーブルの不具合を修正
			_online = new ArrayList<String>();
		}
		return _instance;
	}
	
	
	public void getIpTable() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactoryLogin.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM ok_ip");
			rs = pstm.executeQuery();
			while (rs.next()) {
				_okip.add(rs.getString(1));
				_host.add(rs.getString(2));//TODO ban_ipテーブルの不具合を修正
			}
			isInitialized = true;
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	public boolean isOkIp(final String s) {
		return _okip.contains(s);
	}
	public boolean isOnline(final String s){
		if(_online.contains(s)){
			return true;
		}
		_online.add(s);
		return false;
	}
}
